package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Employee;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Joey
 * @Description:
 * @date:2024/4/15 14:25
 */
@Service
public class DishServiceImp implements DishService {


    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Transactional
    public void insertDish(DishDTO dishDTO) {
        //新增菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        dishMapper.insertDish(dish);

        //获取新增菜品的id 批量插入口味表
        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors !=null && flavors.size() > 0){
            //批量插入id
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.insertFlavorBatch(flavors);
        }

    }

    @Override
    public PageResult dishListPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageDishQuery(dishPageQueryDTO);
        long total = page.getTotal();
        List<DishVO> records = page.getResult();
        return new PageResult(total,records);
    }

    @Override
    @Transactional
    public void modifyStatus(Integer status, long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.dishStatus(dish);
    }

    @Override
    @Transactional
    public void modifyDishInfo(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        dishMapper.modifyDishInfo(dish);

        //先删除 后加
        dishFlavorMapper.deleteFlavorById(dish.getId());

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors !=null && flavors.size() > 0){
            flavors.forEach(df -> {
                df.setDishId(dish.getId());
            });
            dishFlavorMapper.insertFlavorBatch(flavors);
        }

    }

    @Override
    public DishVO selectDishById(Integer id) {
        Dish dish = dishMapper.queryDish(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        List<DishFlavor> list = dishFlavorMapper.queryFlavors(id);
        dishVO.setFlavors(list);
        return dishVO;
    }

    @Override
    public List<Dish> selectDishByCateGoryId(Integer cateGoryId) {
        return dishMapper.queryDishCategory(cateGoryId);
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {

        //判断售卖状态 启售(1) 不可删除
        for (Long id : ids) {
            Dish dish = dishMapper.queryDishL(id);
            if(dish.getStatus() == 1){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //是否套餐关联 套餐关联不可删除
        List<Long> setMealDish = setmealDishMapper.getSetMealDish(ids);
        if(setMealDish !=null && setMealDish.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //批量、删除菜品数据
        dishMapper.deleteBatch(ids);
        //批量、删除菜品对应口味
        dishFlavorMapper.deleteFlavorBatch(ids);

    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.queryFlavors(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
